﻿using AgencyManagerSystem.AsmType;
using AgencyManagerSystem.Common;
using AgencyManagerSystem.Core;
using AgencyManagerSystem.DataFetcher;
using AgencyManagerSystem.Model;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Net;

namespace AgencyManagerSystem.Controllers
{
    public class DeliveryTraceController : MyControllerBase
    {
        DeliveryTraceManager manager;
        DeliveryTraceFetcher fetcher;
        public DeliveryTraceController() : base()
        {
            this.manager = new DeliveryTraceManager();
            this.fetcher = new DeliveryTraceFetcher();
        }
        [HttpGet]
        [RoleFlagAuthorizeAttribute(PolicyDomain.Retail, AppOpt.Exec)]
        public AsmArray<DeliveryTraceModel> List(string jsonCondition, int pos, int size)
        {
            LogHelper.Debug(this.GetType(), String.Format("LIST:[RQS|{1}]{0}", jsonCondition, RequestIp));
            int cnt = 0;
            var deliveryTrace = fetcher.List(Util.ConvertArrayFromJson<Condition>(jsonCondition), pos, size, out cnt);
            DBHelper.Commit( true);
            var result = new AsmArray<DeliveryTraceModel>() { Data = deliveryTrace, Message = "操作成功", Result = AmsCode.Succ, Cnt = cnt };
            LogHelper.Debug(this.GetType(), String.Format("LIST:[RSP|{1}]{0}", Util.ConvertToJson(result), RequestIp));
            return result;
        }
        [HttpGet]
        [RoleFlagAuthorizeAttribute(PolicyDomain.Retail, AppOpt.Exec)]
        public AsmObject<DeliveryTraceModel> Get(int id)
        {
            LogHelper.Debug(this.GetType(), String.Format("GET:[RQS|{1}]{0}", id, RequestIp));
            var deliveryTrace = fetcher.Get(id);
            DBHelper.Commit(true);
            AsmObject<DeliveryTraceModel> result = null;
            if (deliveryTrace != null)
                result = new AsmObject<DeliveryTraceModel>() { Data = deliveryTrace, Message = "操作成功", Result = AmsCode.Succ };
            else
                result = new AsmObject<DeliveryTraceModel>() { Data = deliveryTrace, Message = "无法找到数据", Result = AmsCode.DataNotFound };
            LogHelper.Debug(this.GetType().Name, String.Format("GET:[RSP|{1}]{0}", Util.ConvertToJson(result), RequestIp));
            return result;
        }
        [HttpGet]
        [RoleFlagAuthorizeAttribute(PolicyDomain.Retail, AppOpt.Exec)]
        public AsmArray<DeliveryTraceModel> ListByOrder(int oid)
        {
            LogHelper.Debug(this.GetType(), String.Format("LISTBYORDER:[RQS|{1}]{0}", oid, RequestIp));
            int cnt = 0;
            var deliveryTrace = fetcher.ListByOrder(oid);
            DBHelper.Commit(true);
            var result = new AsmArray<DeliveryTraceModel>() { Data = deliveryTrace, Message = "操作成功", Result = AmsCode.Succ, Cnt = cnt };
            LogHelper.Debug(this.GetType(), String.Format("LIST:[RSP|{1}]{0}", Util.ConvertToJson(result), RequestIp));
            return result;
        }
    }
}